package com.im.service.impl;

import com.im.dao.UserDao;
import com.im.pojo.ImLately;
import com.im.pojo.ImSend;
import com.im.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;

    @Override
    @Transactional(isolation = Isolation.READ_UNCOMMITTED)
    public ImSend sendMessage(String send_id, String receive_id, String uuid) {
        // 添加消息发送记录
        ImSend imSend = new ImSend(send_id, receive_id, uuid);
        userDao.insertSend(imSend);

        // 修改最近联系人消息, 未读数+1
        ImLately imLately1 = new ImLately(send_id, receive_id, uuid, 0);
        userDao.updateLatelyMessage(imLately1);
        userDao.updateLatelyRead(imLately1);
        ImLately imLately2 = new ImLately(receive_id, send_id, uuid, 1);
        userDao.updateLatelyMessage(imLately2);
        userDao.updateLatelyReadN(imLately2);
        return imSend;
    }
}
